home *** CD-ROM | disk | FTP | other *** search
/ 3D Images / 3D Images.iso / programs / amiga / rayshade / libshade / misc.c < prev    next >
C/C++ Source or Header  |  1995-01-12  |  5KB  |  247 lines

  1. /*
  2.  * misc.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * misc.c,v 4.1 1994/08/09 08:04:33 explorer Exp
  17.  *
  18.  * misc.c,v
  19.  * Revision 4.1  1994/08/09  08:04:33  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:17  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0.1.1  92/01/14  18:29:05  cek
  26.  * patch3: Added support for switching cpp on/off.
  27.  * 
  28.  * Revision 4.0  91/07/17  14:46:31  kolb
  29.  * Initial version.
  30.  * 
  31.  */
  32. #include "rayshade.h"
  33. #ifdef RUSAGE
  34. #include <sys/time.h>
  35. #include <sys/resource.h>
  36. #else
  37. #ifdef TIMES
  38. #include <sys/types.h>
  39. #include <sys/times.h>
  40. #include <sys/param.h>
  41. #endif
  42. #endif
  43. #include "options.h"
  44. #include "stats.h"
  45. #include "libcommon/common.h"
  46. #include "viewing.h"
  47. #include "zbuf.h"
  48.  
  49.  
  50. Float RSabstmp;    /* Temporary value used by fabs macro.  Ugly. */
  51. static void RSmessage();
  52.  
  53. #ifdef AMIGA
  54. #ifndef __GNUC__
  55. extern FILE *popen();
  56. #endif
  57. extern FILE *popen2();
  58. #endif
  59.  
  60. /*
  61.  * Open input file and call yyparse().
  62.  */
  63. void
  64. RSReadInputFile()
  65. {
  66.     extern FILE *yyin;    /* lex/yacc file pointer */
  67.     extern char yyfilename[];
  68.  
  69. #if defined(CPPSTDIN) && defined(POPEN)
  70.     char cmd[BUFSIZ];
  71.  
  72. #ifdef AMIGA
  73.     if (Options.SAScpp) {
  74.     if (Options.cppargs != (char *)NULL)
  75.         sprintf(cmd, "%s %s ", SASCPPSTDIN, Options.cppargs);
  76.     else
  77.         /* fromstdin */
  78.         sprintf(cmd, "%s %s ", SASCPPSTDIN, SASCPPMINUS);
  79.     }else{
  80. #endif
  81.     if (Options.cppargs != (char *)NULL)
  82.         sprintf(cmd, "%s %s ", CPPSTDIN, Options.cppargs);
  83.     else
  84.         /* fromstdin */
  85.         sprintf(cmd, "%s %s ", CPPSTDIN, CPPMINUS);
  86. #ifdef AMIGA
  87.     }
  88. #endif
  89.  
  90.     if (Options.inputname == (char *)NULL) {
  91.         (void)strcpy(yyfilename, "stdin");
  92.     } else {
  93.         (void)strcpy(yyfilename, Options.inputname);
  94.         (void)strcat(cmd, Options.inputname);
  95.     }
  96.  
  97.     if (Options.cpp) {
  98. #ifdef AMIGA
  99.         if (Options.SAScpp)
  100.           yyin = popen2(cmd);
  101.         else
  102. #endif
  103.         yyin = popen(cmd, "r");
  104.         if (yyin == (FILE *)NULL)
  105.             RLerror(RL_PANIC, "popen of \"%s\" failed!\n", cmd);
  106.     } else {
  107. #endif
  108.     if (Options.inputname == (char *)NULL) {
  109.         yyin = stdin;
  110.         (void)strcpy(yyfilename, "stdin");
  111.     } else {
  112.         (void)strcpy(yyfilename, Options.inputname);
  113.         yyin = fopen(Options.inputname, "r");
  114.         if (yyin == (FILE *)NULL)
  115.             RLerror(RL_PANIC,
  116.                 "Cannot open %s.\n",Options.inputname);
  117.     }
  118. #if defined(CPPSTDIN) && defined(POPEN)
  119.     }
  120. #endif
  121.     /*
  122.      * Initialize symbol table.
  123.      */
  124.     SymtabInit();
  125.     if (Options.zbufprint)
  126.         zbuffer.rayfile = strsave (yyfilename);
  127.     (void)yyparse();
  128. }
  129.  
  130. void
  131. OpenStatsFile()
  132. {
  133.     if (Options.statsname == (char *)NULL || Stats.fstats != stderr)
  134.         return;        /* Not specified or already opened. */
  135.  
  136.     Stats.fstats = fopen(Options.statsname, "w");
  137.     if (Stats.fstats == (FILE *)NULL) {
  138.         RLerror(RL_PANIC,
  139.             "Cannot open stats file %s.\n", Options.statsname);
  140.     }
  141. }
  142.  
  143. void
  144. RLerror(level, pat, arg1, arg2, arg3)
  145. int level;
  146. char *pat, *arg1, *arg2, *arg3;
  147. {
  148.     switch (level) {
  149.         case RL_ADVISE:
  150.             if (!Options.quiet)
  151.                 RSmessage("Warning", pat, arg1, arg2, arg3);
  152.             break;
  153.         case RL_WARN:
  154.             RSmessage("Warning", pat, arg1, arg2, arg3);
  155.             break;
  156.         case RL_ABORT:
  157.             RSmessage("Error", pat, arg1, arg2, arg3);
  158.             exit(1);
  159.             break;
  160.         case RL_PANIC:
  161.             RSmessage("Fatal error", pat, arg1, arg2, arg3);
  162.             exit(2);
  163.             break;
  164.         default:
  165.             RSmessage("Unknown error", pat, arg1, arg2, arg3);
  166.             exit(3);
  167.     }
  168. }
  169.  
  170. static void
  171. RSmessage(type, pat, arg1, arg2, arg3)
  172. char *type, *pat, *arg1, *arg2, *arg3;
  173. {
  174.     extern FILE *yyin;
  175.     extern int yylineno;
  176.     extern char yyfilename[];
  177.  
  178.     if (yyin) {
  179.         /*
  180.          * cleanup() hasn't nulled yyin, so line #
  181.          * info is valid.
  182.          */
  183.         fprintf(stderr,"%s: %s: %s, line %d: ",
  184.             Options.progname, type,
  185.             yyfilename == (char *)NULL ? "stdin" :
  186.                 yyfilename, yylineno);
  187.     } else {
  188.         fprintf(stderr,"%s: %s: ", Options.progname, type);
  189.     }
  190.     fprintf(stderr, pat, arg1, arg2, arg3);
  191. }
  192.         
  193. #ifdef RUSAGE
  194. void
  195. RSGetCpuTime(usertime, systime)
  196. Float *usertime, *systime;
  197. {
  198.     struct rusage usage;
  199.  
  200.     getrusage(RUSAGE_SELF, &usage);
  201.  
  202.     *usertime = (Float)usage.ru_utime.tv_sec +
  203.             (Float)usage.ru_utime.tv_usec / 1000000.;
  204.     *systime = (Float)usage.ru_stime.tv_sec +
  205.             (Float)usage.ru_stime.tv_usec / 1000000.;
  206. }
  207.  
  208. #else
  209. #ifdef TIMES
  210.  
  211. void
  212. RSGetCpuTime(usertime, systime)
  213. Float *usertime, *systime;
  214. {
  215.     extern CLOCKTYPE times();
  216.     struct tms time;
  217.  
  218.     (void)times(&time);
  219.     *usertime = (Float)time.tms_utime / (Float)HZ;
  220.     *systime = (Float)time.tms_stime / (Float)HZ;
  221. }
  222.  
  223. #else /* !RUSAGE && !TIMES */
  224. #ifdef AMIGA
  225. void
  226. RSGetCpuTime(usertime, systime)
  227. Float *usertime, *systime;
  228. {
  229.     long c[2];
  230.     extern long InitTime[2];
  231.  
  232.     timer(c);
  233.     *usertime = (Float)c[0] + (Float)c[1]/1000000.0 -
  234.             ((Float)InitTime[0] + (Float)InitTime[1]/1000000.0);
  235.     *systime = 0;
  236. }
  237. #else
  238. void
  239. RSGetCpuTime(usertime, systime)
  240. Float *usertime, *systime;
  241. {
  242.     *usertime = *systime = 0.;
  243. }
  244. #endif
  245. #endif /* TIMES */
  246. #endif /* RUSAGE */
  247.